"""
Case Type   : Trigger
Case Name   : MOT不支持触发器
Create At   : 2020/8/2
Owner       : @li-xin12345
Description :
    1、修改enable_incremental_checkpoint=off
    2、重启数据库,gs_om -t stop && gs_om -t start
    3、创建内存表外表&源表，创建触发器函数，创建触发器；
    4、清理数据;
Expect      :
    1、修改成功；
    2、重启数据库成功；
    3、创建表成功，创建触发器函数成功，创建触发器失败；
    4、清理环境成功；
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common

LOG = Logger()
CONSTANT = Constant()
Primary_SH = CommonSH('PrimaryDbUser')


class TriggerMotTest(unittest.TestCase):
    def setUp(self):
        LOG.info("======Opengauss_Function_Trigger_Case0055开始执行======")
        self.common = Common()
        self.t_name1 = 't_trigger_0055_01'
        self.t_name2 = 't_trigger_0055_02'
        self.f_name = 'f_trigger_0055'
        self.tri_name = 'tri_trigger_0055'

    def test_trigger_mot(self):
        LOG.info("======检查参数，修改配置，并重启数据库======")
        self.config_item = 'enable_incremental_checkpoint=off'
        self.enable_incremental_checkpoint = self.common.show_param(
            'enable_incremental_checkpoint')
        if self.enable_incremental_checkpoint != 'off':
            Primary_SH.execute_gsguc('set', CONSTANT.GSGUC_SUCCESS_MSG,
                                     self.config_item)
            result = Primary_SH.restart_db_cluster()
            LOG.info(result)
            self.assertTrue(result)
        LOG.info("=======步骤1：创建内存表 外表&源表=======")
        sql_cmd1 = f'drop foreign table if exists {self.t_name1};' \
                   f'drop foreign table if exists {self.t_name2};' \
                   f'create foreign table {self.t_name1}(id1 int,id2 int,' \
                   f'id3 int);' \
                   f'create foreign table {self.t_name2}(id1 int,id2 int,' \
                   f'id3 int);'
        LOG.info(sql_cmd1)
        create_res1 = Primary_SH.execut_db_sql(sql_cmd1)
        LOG.info(create_res1)
        self.assertEqual(create_res1.count(
            CONSTANT.DROP_FOREIGN_SUCCESS_MSG), 2)
        self.assertEqual(create_res1.count(
            CONSTANT.CREATE_FOREIGN_SUCCESS_MSG), 2)

        LOG.info("======步骤2：创建触发器函数 & 触发器======")
        create_cmd2 = f'''
            create or replace function {self.f_name}() returns trigger
            as
            \$\$
            declare
            begin
            insert into {self.t_name2} values(NEW.id1,NEW.id2,NEW.id3);
            return NEW;
            end
            \$\$ language plpgsql;
            create trigger {self.tri_name} before insert on
            {self.t_name1} for each row execute procedure {self.f_name}();
            '''
        LOG.info(create_cmd2)
        create_res2 = Primary_SH.execut_db_sql(create_cmd2)
        LOG.info(create_res2)
        self.assertIn('not a table or view', create_res2)

    def tearDown(self):
        LOG.info("======清理环境,恢复配置======")
        del_cmd = f'drop foreign table {self.t_name1};' \
                  f'drop foreign table {self.t_name2};' \
                  f'drop function {self.f_name}();'
        LOG.info(del_cmd)
        del_msg = Primary_SH.execut_db_sql(del_cmd)
        LOG.info(del_msg)
        result = Primary_SH.execute_gsguc(
            'set', CONSTANT.GSGUC_SUCCESS_MSG,
            f'enable_incremental_checkpoint='
            f'{self.enable_incremental_checkpoint}')
        LOG.info(result)
        self.assertEqual(del_msg.count(CONSTANT.DROP_FOREIGN_SUCCESS_MSG), 2)
        self.assertIn(CONSTANT.DROP_FUNCTION_SUCCESS_MSG, del_msg)
        self.assertTrue(result)
        LOG.info("======Opengauss_Function_Trigger_Case0055执行结束======")
